//
//  OrderViewController.m
//  Doctors
//
//  Created by 王森 on 16/4/7.
//  Copyright © 2016年 王森. All rights reserved.
//

#import "OrderViewController.h"
#import "LXDSegmentControl.h"
#import "OrderViewTableViewCell.h"
#import "OrderModel.h"
#import "MJRefresh.h"
#import "OrderDetailViewController.h"
@interface OrderViewController ()<LXDSegmentControlDelegate>
{
    NSString *_artisanId;
    int page;
    int selectIndex;
    
}
@end

@implementation OrderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
      [self setTextTitleViewWithFrame:CGRectMake(0, 0, 60, 20) title:@"订单" fontSize:18];
    _artisanId=[[TMCache sharedCache]objectForKey:@"artisanId"];

    [self makeUI];

//    [self requestData:@"1"];
    
  
    
    // Do any additional setup after loading the view from its nib.
}

-(void)makeUI
{
    page=1;
    
    
    CGRect frame = CGRectMake(0,64, SCREEN_WIDTH, 48);
    NSArray * items = @[@"个人订单", @"企业订单", @"地推活动单"];
    
    LXDSegmentControlConfiguration * slide = [LXDSegmentControlConfiguration configurationWithControlType: LXDSegmentControlTypeSlideBlock items: items];
    slide.itemTextColor=UIColorFromRGB(0x909090);
    slide.itemSelectedTextColor=UIColorFromRGB(0x53cac3);
    slide.itemTextFont=[UIFont systemFontOfSize:14];
    
    slide.slideBlockColor=UIColorFromRGB(0x53cac3);
    LXDSegmentControl * slideControl = [LXDSegmentControl segmentControlWithFrame: frame configuration: slide delegate: self];
    slideControl.backgroundColor=[UIColor whiteColor];
    [self.view addSubview: slideControl];
    
    
    [self customTableView];
    self.tableview.frame=CGRectMake(0, slideControl.bottom, SCREEN_WIDTH, ScreenHeight-64-48-48);
  

    UINib *nib=[UIView loadNibNamed:@"OrderViewTableViewCell" bundle:nil];
    
    [self.tableview registerNib:nib forCellReuseIdentifier:@"OrderViewTableViewCell"];
    __unsafe_unretained __typeof(self) weakSelf = self;

    self.tableview.mj_header= [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self.dataArray removeAllObjects];
        page=1;
        
        if (selectIndex==10) {
            [weakSelf requestData:@"1"];
            
        }
        
        if (selectIndex==20) {
            [weakSelf requestData:@"3"];
            
        }
        
        if (selectIndex==30) {
            [weakSelf requestData:@"2"];
            
        }

    }];
    

    self.tableview.mj_footer=[MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        page++;
        
        if (selectIndex==10) {
            [weakSelf requestData:@"1"];
 
        }
        
        if (selectIndex==20) {
            [weakSelf requestData:@"3"];
   
        }
        
        if (selectIndex==30) {
            [weakSelf requestData:@"2"];
            
        }
    }];
     

}

- (void)segmentControl: (LXDSegmentControl *)segmentControl didSelectAtIndex: (NSUInteger)index
{
    [self.dataArray removeAllObjects];
    
    page=1;
    
    if (index==0) {
        selectIndex=10;
        
        [self requestData:@"1"];
        
    }
    if (index==1) {
        selectIndex=20;
        [self requestData:@"3"];
   
    }
    if (index==2) {
        selectIndex=30;
        
        [self requestData:@"2"];
        
    }
}


-(void)requestData:(NSString * )type
{
    
    
    [self addProgressHUD];
    
    NSDictionary *dictionary=[NSDictionary dictionaryWithObjectsAndKeys:_artisanId, @"artisanId",@"0",@"status",type,@"type",[NSString stringWithFormat:@"%d",page],@"page", nil];
    NSLog(@"订单列表参数：%@",dictionary);
    [[WSNetworkApi shareManager]postWithUrl:@"/order/queryOrderList" parameters:dictionary option:RequestCenterCachePolicyNormal sucess:^(id responseObject) {
        
        [self removeStatuslable];

        NSLog(@"%@",responseObject);
        
        if ([responseObject[@"status"] isEqualToString:@"ok"]) {
           
            NSArray *array=responseObject[@"orderList"];
            
            if (array.count<1) {
                page--;
                [self.view makeToast:@"没有数据了"];
            }
            for (NSDictionary *dic in array) {
                OrderModel *model=[[OrderModel alloc]initWithDic:dic];
//                NSLog(@"订单的支付状态%d",model.payType);
                
                [self.dataArray addObject:model];
                
            
            }
            
            
        }
        else{
            [self showFailed:responseObject[@"message"]];
            
        }
        
        [self.tableview reloadData];
        [self.tableview.mj_header endRefreshing];

        [self.tableview.mj_footer endRefreshing];

    } failur:^(NSError *error) {
        page--;
        [self.tableview.mj_footer endRefreshing];
        [self.tableview.mj_header endRefreshing];

    }];
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    __weak typeof(self)weakSelf=self;
//    
//    OrderModel *model=self.dataArray[indexPath.row];
    
        OrderViewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"OrderViewTableViewCell" forIndexPath:indexPath];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
    [cell cellFuzhi:self.dataArray[indexPath.row]];
    
    __weak typeof(self)weakSef=self;
    
    [cell setReRqquest:^{
        [self.dataArray removeAllObjects];
        
        [weakSef requestData:@"1"];

    }];
    
    if (selectIndex==30||selectIndex==20   ) {
        
        cell.status.hidden=YES;
        
        
    }
    else{
        cell.status.hidden=NO;

    }
    
    return cell;
    
    }

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    OrderModel *model=nil;
    
    if (self.dataArray.count!=0) {
      model =self.dataArray[indexPath.row];
    }
   
    NSString *status = [NSString stringWithFormat:@"%d",model.orderStatus];
    if (![status isEqualToString:@"0"]&&![status isEqualToString:@"10"]&&![status isEqualToString:@"20"]) {
        return 154;
        
    }
    else
    {
        return 210;
 
    }
        
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    OrderModel *model=self.dataArray[indexPath.row];
    
    ALLOC(OrderDetailViewController, view);
    view.orderId=model.orderId;
    view.hidesBottomBarWhenPushed=YES;
//    [Singleton shareSingleton].orderId=model.orderId;
    
    
    
      [self pushNextViewController:view];
    
    NSLog(@"请重写方法");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
